﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lm.Common.DAL.ModelCodeGenerator.PropertyGenerator;

namespace Lm.Common.DAL.ModelCodeGenerator
{
    class Generator : IGenerator
    {
        public string aa { get; set; }
        public void Generate(string dir, string nameSpace, List<Model> modelList,List<string> excludedTableList)
        {
            var dictExcludedTable = excludedTableList.ToDictionary(t => t.ToLower());
            foreach (var model in modelList)
            {
                if (dictExcludedTable.ContainsKey(model.Name))
                {
                    continue;
                }
                using (var sw = new System.IO.StreamWriter(dir + "\\" + model.Name + ".cs", false, Encoding.UTF8))
                {
                    sw.WriteLine("using System;");
                    sw.WriteLine();
                    sw.WriteLine("namespace " + nameSpace);
                    sw.WriteLine("{");
                    sw.WriteLine("    public class " + model.Name + " : Lm.Common.DAL.IModelBase");
                    sw.WriteLine("    {");
                    foreach (var property in model.ModelPropertyList)
                    {
                        
                        sw.WriteLine("        " + this.ParseProperty(property));
                        sw.WriteLine();
                    }
                    sw.WriteLine("    }");
                    sw.WriteLine("}");
                }
            }
        }

        protected string ParseProperty(ModelProperty property)
        {
            var propertyGenerator = (IPropertyGenerator)System.Reflection.Assembly.GetExecutingAssembly()
                .CreateInstance("Lm.Common.DAL.ModelCodeGenerator.PropertyGenerator." + property.Type.ToLower() + "Generator");
            return propertyGenerator.Generate(property);
        }
    }
}
